package com.gpjcjl.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.ViewModelProvider;

import com.base.lib.IntentUtil;
import com.base.lib.ToastUtil;
import com.gpjcjl.App;
import com.gpjcjl.BaseActivity;
import com.gpjcjl.R;

import butterknife.OnClick;

/**
 * 首页
 *
 * @author yinbiao
 */
public class MainActivity extends BaseActivity {

    private MainViewModel viewModel;

    @Override
    protected int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        viewModel = new ViewModelProvider(this).get(MainViewModel.class);

        init();
    }

    private void init() {
        showLoading();
        viewModel.init().observe(this, result -> {
            dismissLoading();
            if (result.getCode() == 2) {
                showActiveDialog();
            } else if (!result.isSuccess()) {
                ToastUtil.showToast(mContext, result.getMsg());
            }
        });
    }

    @SuppressLint("HardwareIds")
    private void showActiveDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("提示")
                .setCancelable(false)
                .setMessage("设备编号：" + App.getInstance().getAndroidId() + "\n该设备未激活，请联系管理员")
                .setPositiveButton("退出", (dialog, which) -> {
                    dialog.cancel();
                    finish();
                })
                .create().show();
    }

    @OnClick({R.id.iv_setting, R.id.create, R.id.history})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_setting:
                IntentUtil.startIntent(mContext, SettingActivity.class);
                break;
            case R.id.create:
                IntentUtil.startIntent(mContext, RecordCreateActivity.class);
                break;
            case R.id.history:
                IntentUtil.startIntent(mContext, HistoryActivity.class);
                break;
            default:
                break;
        }
    }
}